//
//  AliPayTool.m
//  CityGuards
//
//  Created by striby on 16/5/11.
//  Copyright © 2016年 kunyang. All rights reserved.
//

#import "AliPayTool.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>

@implementation Product



@end
@implementation AliPayTool
+(NSString*)UUID
{
    NSString *result = nil;
    
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    if (uuid)
    {
        result = ( NSString *)CFBridgingRelease(CFUUIDCreateString(NULL, uuid));
        CFRelease(uuid);
    }
    return result;
}

+(void)AliPayForTheProduct:(Product *)product payCompletion:(payCompletion)completion
{

    
    /*
     *商户的唯一的parnter和seller。
     *签约后，支付宝会为每个商户分配一个唯一的 parnter 和 seller。
     */
    
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088421504312985";
    NSString *seller = @"13701568670@163.com";
    NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAP4GNUIwvOatgDvNND6s4ZvVOK51X1p+7SECsxn9Kc6xTCvei2bHQGfvoItRTkw+jkAUogVHuKsjwpu2JFAd0p2aDD3nl9cpc5k5OtS9TwcQT7w3cskEpz5Sk/1tV/j+KwaspUS02NzoB6+v63DT2MpXsjQaMAJ5dlF/snggl7wLAgMBAAECgYBScoNgu17kZBXF0yGM4iHLqVXfUdskDSQ73Ztx6PhjzKnTZ0TJlAw0b9MkxLvsBTW4WLoJirDGYzHrxh9dHYDY3rhX3fy8gYWkXyInGveGfd4JqLmadXM3QVI9SgyQlZ4wb20izV4j8cOlDJNPeikgmEsVPMloRKV8Cvf2AG6ViQJBAP+AltyKjDQWaYcjnhzljoq3JKyUh0tqJRr3PL7hbANAQqYb/qy2LKaVlS/lWzSrl4pPhm6daAMqqXI9uTTMZEUCQQD+hOG164EQwRdsZpgupeTSU257OCUp2woeYXKzwqQ0sEYwwsAx1onK2yw5qW/nZ4MC+EsOPcE6CdcCIYLM+iwPAkAGMeL2g/F0TdrPKQDraHF1UY11FcDWBkPSDI6Pfj21MnirOGCLZXtdqqDMYX49n69z7guvYOeltAIZv/9elVoVAkEAw67AppCZW5tJegNrD7pmMjsJH11sXUDWB8OeX4zPmN7KKGUjRW4P5NIbr32Q1tYjyR3C/wEF1ayJDU9cdGIHiwJAExX9LzJ3j7W/TWkwNG0U9Dtucw/7vFq4nn3mcPGOtcxtGzUvQpMColJQj7kwv+dHkiAGtFEm3Yydqfd7yO30vA==";
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 ||
        [seller length] == 0 ||
        [privateKey length] == 0)
    {
        
        NSLog(@"缺少必要信息");
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.sellerID = seller;
    
    order.outTradeNO = product.orderId;//订单ID（由商家自行制定）
    
    order.subject = product.subject; //商品标题
    order.body = product.body; //商品描述
    order.totalFee = [NSString stringWithFormat:@"%.2f",product.price]; //商品价格
    order.notifyURL =  @"http://221.224.118.52/cityManage/alipay/pay.cw"; //回调URL

  
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showURL = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme =
    @"citysafe";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
            
            
            switch ([resultDic[@"resultStatus"] integerValue]) {
                //中途取消
                case 6001:
                {
                    completion(DCPayStateInterrupt);
                }
                    break;
                    
                //网络出错
                case 6002:
                {
                    completion(DCPayStatePayeFailed);
                }
                    break;
                    
                //支付失败
                case 4000:
                {
                    completion(DCPayStatePayeFailed);
                }
                    break;
                
                //正在处理
                case 8000:
                {
                    completion(DCPayStatePayeProgressing);
                }
                    break;
                    
                //支付成功
                case 9000:
                {
                    completion(DCPayStatePayedSuccess);
                }
                    break;
                    
                    
                default:
                    break;
            }
            
        }];
    }

}

@end
